/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.interaction;

import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import de.fuberlin.processing.who.cancer.utils.Test;


public class LocationBroadcaster extends Observable implements LocationModifier {

	private static LocationBroadcaster locationBroadcaster = null;
	
	private Vector<Integer> locations = new Vector<Integer>();
	
	
	private LocationBroadcaster() {
		super();
	}
	
	
	public static LocationBroadcaster get() {
		if ( Test.isUnassigned( locationBroadcaster)) {
			locationBroadcaster = new LocationBroadcaster();
		}
		return locationBroadcaster;
	}
	

	@Override
	public boolean addLocation( Integer locationId) {
		if ( !this.locations.contains( locationId)) {
			this.locations.add( locationId);
			Vector<Integer> locationsToAdd = new Vector<Integer>();
			locationsToAdd.add( locationId);
			super.setChanged();
			super.notifyObservers( new LocationChangeSet(
					LocationModifications.ACTION_ADD, locationsToAdd));
			return true;
		}
		return false;
	};


	@Override
	public boolean removeLocation( int locationId) {
		if ( locations.contains( locationId)) {
			locations.remove( locationId);
			Vector<Integer> locationsToDelete = new Vector<Integer>();
			locationsToDelete.add( locationId);
			super.setChanged();
			super.notifyObservers( new LocationChangeSet(
					LocationModifications.ACTION_DELETE, locationsToDelete));
			return true;
		}
		return false;
	}


	@Override
	public boolean addLocations( Collection<Integer> locations) {
		if ( Test.isAssigned( locations)) {
			Vector<Integer> locationsToAdd = new Vector<Integer>();
			for ( Integer location : locations) {
				if ( !this.locations.contains( location))
					locationsToAdd.add( location);
			}
			if ( Test.isCollectionContainingElements( locationsToAdd)) {
				this.locations.addAll( locationsToAdd);
				super.setChanged();
				super.notifyObservers( new LocationChangeSet(
						LocationModifications.ACTION_ADD, locationsToAdd));
				return true;
			}
			return false;
		}
		return false;
	};


	@Override
	public void clear() {
		this.locations.clear();
		super.setChanged();
		super.notifyObservers( new LocationChangeSet(
				LocationModifications.ACTION_CLEAR, null));
	}


	@Override
	public void setLocations( Collection<Integer> locations) {
		this.locations.clear();
		Vector<Integer> locationsToAdd = new Vector<Integer>();
		if ( Test.isAssigned( locations)) {
			for ( Integer location : locations) {
				if ( !this.locations.contains( location))
					locationsToAdd.add( location);
			}
		}

		if ( Test.isCollectionContainingElements( locationsToAdd)) {
			this.locations.addAll( locationsToAdd);
		}

		super.setChanged();
		super.notifyObservers( new LocationChangeSet(
				LocationModifications.ACTION_SET, locationsToAdd));
	}


	@Override
	public Collection<Integer> getLocations() {
		Vector<Integer> copyOfLocations = new Vector<Integer>( this.locations);
		return copyOfLocations;
	}


	@Override
	public void update() {
		super.setChanged();
		super.notifyObservers( new LocationChangeSet(
				LocationModifications.ACTION_UPDATE, this.locations));
	}
	

}
